package com.cyf.cheyichong.service;

import com.cyf.cheyichong.commonutils.R;
import com.cyf.cheyichong.entity.User;
import com.cyf.cheyichong.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public R login(User user) {
        Long userId = userMapper.getUserId(user);
        if (userId == null)
            return R.error().message("用户名或密码错误！");
        return R.ok().message("登录成功！");
    }

    public R regist(User user) {
        User existUser = userMapper.getUserByUserPhone(user.getUserPhone());
        if (existUser == null) {
            userMapper.addUser(user);
            return R.ok();
        } else {
            return R.error().message("用户已存在！");
        }
    }
}